<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="layout" content="bocMain"/>
    <title>进件作业系统</title>
</head>

<body>

<div class="wxLogin">
    <div class="logo">
        <asset:image src="/boc/bank-logo.png" class="weui-media-box__thumb" alt=""/>
        <p>进件作业系统</p>
    </div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-flex weui-login">
            <div class="weui-cell__hd"><label class="weui-label weui-label-img" style="margin-top: -2px"><asset:image src="/boc/tel.png" class="weui-media-box__thumb" alt=""/></label></div>
            <div class="weui-cell__bd">
                <g:field class="weui_input weui_input_tel" name="cellphone" id="cellphone" type="number" pattern="[0-9]*" autofocus="autofocus" placeholder="请输入手机号" value="${this?.user?.cellphone}"/>
            </div>
        </div>
        <div class="weui-flex weui-login">
            <div class="weui-cell__hd"><label class="weui-label weui-label-img"><asset:image src="/boc/password.png" class="weui-media-box__thumb" alt=""/></label></div>
            <div class="weui-cell__bd">
                <g:field class="weui_input weui_input_tel" style="width: 12rem" type="number" placeholder="请输入验证码" name="verificationCode" id="verificationCode"/>
            </div>
            <div class="weui-cell__bd" style="margin-right: 4px">
                <input class="weui-input weui-input-vCode" id="btnSendVerifiedCode" type="button" class="vCode-btn weui-btn weui-btn_mini weui-btn_primary" value="获取验证码"/>
            </div>
        </div>
    </div>

    <div class="bigBtn">
        <button type="button" class="weui-btn weui-btn_primary" id="submitBtn">立即登录</button>
    </div>
</div>

<script>
    $(function () {
        var time = 30;
        var interval;
        $('#btnSendVerifiedCode').click(function () {
            if ($("#cellphone").val().length == 0) {
                $(".helpMsg").text("请输入手机号").fadeIn(200);
                $('#btnSendVerifiedCode').addClass("vCodeActive").attr('disabled', 'disabled');
                setTimeout(function () {
                    $(".helpMsg").fadeOut(200);
                    $('#btnSendVerifiedCode').removeClass("vCodeActive").removeAttr('disabled');
                }, 2000);
                return;
            }
            if (/^1\d{10}$/.test($("#cellphone").val())) {
                $.ajax({
                    type: "POST",
                    url: "/bocAppOpportunity/sendVerifiedCode",
                    data: {
                        cellphone: $("#cellphone").val(),
                    },
                    beforeSend: function () {
                        interval = setInterval(function () {
                            if (time > 0) {
                                $('#btnSendVerifiedCode').attr('disabled', 'disabled').addClass("vCodeActive");
                                $('#btnSendVerifiedCode').val("" + (time--) + '秒后重试');
                            } else {
                                $('#btnSendVerifiedCode').removeAttr('disabled').removeClass("vCodeActive");
                                $('#btnSendVerifiedCode').val("获取验证码");
                                time = 60;
                                clearInterval(interval);
                            }
                        }, 1000);

                    },
                    success: function (data) {
                        if (data.status == "success") {
                            helpMessage("验证码发送成功");
                        }
                        if (data.status == "error") {
                            $(".helpMsg").text(data.errorMessage).fadeIn(200);
                            setTimeout(function () {
                                $(".helpMsg").fadeOut(200);
                            }, 2000);
                        }
                    }
                });
            }
            else
            {
                helpMessage("请输入正确的手机号");
            }
        });
        setTimeout(function () {
            $(".message").fadeOut(200);
        }, 2000);

        $("#submitBtn").click(function ()
        {
            var cellphoneStr = $("#cellphone").val().trim();
            var verificationCodeStr = $("#verificationCode").val().trim();
            if (cellphoneStr.length == 0)
            {
                helpMessage("请输入手机号");
                return;
            }
            if (!(/^1\d{10}$/.test(cellphoneStr)))
            {
                helpMessage("请输入正确的手机号");
                return;
            }
            if (!verificationCodeStr)
            {
                helpMessage("请输入验证码");
                return;
            }

            $.ajax({
                url: "/login/authenticate",
                type: "post",
                data: "username=" + $("#cellphone").val() + "&password=" + $("#verificationCode").val(),
                success: function (data) {
                    if (data.success)
                    {
                        location.href = "/bocAppOpportunity/businessManagement";
                    }
                    else
                    {
                        helpMessage('用户名或验证码错误，请重试!');
                    }
                },
                error: function (e) {
                    console.log(e)
                    $.ajax({
                        url: "/bocAppOpportunity/isLogIn",
                        type: "post",
                        success: function (data) {
                            if (data.status == 'success')
                            {
                                location.href = "/bocAppOpportunity/businessManagement";
                            }
                            else
                            {
                                helpMessage('用户名或验证码错误，请重试!');
                            }
                        },
                        error: function (e) {
                            helpMessage('用户名或验证码错误，请重试!');
                        },
                    });
                },
            });
        });

        function helpMessage(message) {
            $(".helpMsg").text(message).fadeIn(200);
            setTimeout(function () {
                $(".helpMsg").fadeOut(200);
            }, 2000);
        }
    });

</script>
</body>
</html>
